/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.ListsProvider;
import ge.info.domain.MainResultDetail;
import ge.info.domain.Organization;
import ge.ktapestry.pages.BasePageExt;
import ge.ktools.StringUtils;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

public abstract class OrgResultPage extends BasePageExt implements PageBeginRenderListener
{

    @InjectObject("spring:listsProvider")
    public abstract ListsProvider getListsProvider();

    @Persist
    public abstract Organization getOrganization();

    public abstract void setOrganization(Organization organization);

    @InitialValue("null")
    public abstract Integer getOrganizationID();

    public abstract void setOrganizationID(Integer organizationID);

    @Persist
    public abstract Integer getOid();

    public abstract void setOid(Integer oid);


    public abstract MainResultDetail getDetail();

    public abstract void setDetail(MainResultDetail mainResultDetail);
     //@Persist
    public abstract Organization getSlave();

    public abstract void setSlave(Organization o);

    public static int SERVICE_ID = 3;


    public void pageBeginRender(PageEvent event)
    {

        if (!event.getRequestCycle().isRewinding())
        {
            setOid(getOrganizationID());

            if (getOrganization() == null)
            {
                setOrganization(getDbExecutor().getOrganizationByID(getOrganizationID()));
            }
            getDbExecutor().getOrganizationDetails(getOrganization());
            getDbExecutor().fillOrganizationMasterSlaves(getOrganization());
            getDbExecutor().logHits(getSession(), getOrganization().getMainID(), SERVICE_ID);
        }
        return;
    }

    public void OpenOrgResultInfo(IRequestCycle cycle, Integer orgID)
    {
        IPage page = cycle.getPage("OrgResultInfo");
        PropertyUtils.write(page, "organizationID", orgID);
        PropertyUtils.write(page, "organization", null);
        cycle.activate(page);
    }

    public void openOrgResultPage(IRequestCycle cycle, Integer orgID)
    {

       // Integer orgID = id;   //Integer.valueOf(id);
        IPage page = cycle.getPage("OrgResultPage");
        PropertyUtils.write(page, "organizationID", orgID);
        PropertyUtils.write(page, "organization", null);
        cycle.activate(page);
    }

     public void openMainResultPage(IRequestCycle cycle, String id)
    {

        Integer orgID = Integer.valueOf(id);
        IPage page = cycle.getPage("OrgResultPage");
        PropertyUtils.write(page, "organizationID", orgID);
        PropertyUtils.write(page, "organization", null);
        cycle.activate(page);
    }


    public void sendSMS(IRequestCycle cycle, MainResultDetail detail)
    {

        Organization org = getOrganization();
        StringBuilder sb = new StringBuilder();

        if (org.getLegalstatuse() != null)
        {
        sb.append(org.getLegalstatuse()).append(" ");
        }
        sb.append(org.getName()).append("; ");

         if (detail.getMainDetail() != null)
        {
             if (detail.getMainDetail().equals("საერთო"))
             {
             sb.append("ტელ: ");
             }
             else
             {
            sb.append(detail.getMainDetail()).append(" ");
             }
        }

        if (detail.getContactPhones() != null)
        {
            if (detail.getContactPhones().length()==6)
            {
               sb.append(detail.getContactPhones()).append(" ");
            }
            else
            {
            sb.append(StringUtils.toNumbers(detail.getContactPhones())).append(" ");
            }
        }
        if (detail.getPhones() != null)
        {
             if (detail.getPhones().length()==6)
            {
               sb.append(detail.getPhones()).append(" ");
            }
            else
            {
            sb.append(StringUtils.toNumbers(detail.getPhones()));
            }
         }


        if (org.getAddress().getStreetName() != null)
        {
            sb.append(org.getAddress().getStreetName()).append("; ");
        }

          if (org.getMail() != null)
        {
            sb.append(org.getMail()).append("; ");
        }

        if (org.getWebaddress() != null)
        {
            sb.append(org.getWebaddress()).append("; ");
        }

        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }
   
}
